using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(IVGIBaker))]
public class IVGIBakerInspector : Editor
{
    IVGIBaker _IVGIBaker;
    
    void OnEnable() => _IVGIBaker = (IVGIBaker)target;

    public override void OnInspectorGUI()
    {
        if (target == null)
        {
            return;
        }

        serializedObject.Update();

        PlacementProbeInspector();

        BakeInspector();
        
        TestInspector();
        
        SceneView.RepaintAll();
        serializedObject.ApplyModifiedProperties();
    }

    private void PlacementProbeInspector()
    {
        GUILayout.Label("创建Light Probe", new GUIStyle(EditorStyles.boldLabel));
        
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_LightProbeGroup"), new GUIContent("LightProbeGroup："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_VolumeOrigin"), new GUIContent("VolumeOrigin："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_VolumeSize"), new GUIContent("VolumeSize："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_LightProbeGroupDensity"), new GUIContent("Light Probe 密度："));
        
        if (GUILayout.Button("Placement Probe"))
        {
            _IVGIBaker.PlacementProbe();
        }
    }

    private void BakeInspector()
    {
        EditorGUILayout.Space();
        
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_MeshRoot"),new GUIContent("建筑根节点："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_TextureSize"),new GUIContent("烘培纹理尺寸："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_IVGILightMap3D"),new GUIContent("_IVGILightMap3D："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_IVGILightMap2D"),new GUIContent("_IVGILightMap2D："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_ShadowMapRT"),new GUIContent("_ShadowMapRT："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_ShadowMap3D"),new GUIContent("_ShadowMap3D："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_MaterialLit"),new GUIContent("_MaterialLit："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("_MaterialIVGI"),new GUIContent("_MaterialIVGI："));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("shadowCompute"),new GUIContent("shadowCompute："));

        EditorGUILayout.BeginHorizontal();
        
        if (GUILayout.Button("Bake IVGI"))
        {
            _IVGIBaker.CombineMesh();
            _IVGIBaker.BakeIVGI();
        }

        if (GUILayout.Button("Bake BakeSH2IVGI"))
        {
            _IVGIBaker.CombineMesh();
            _IVGIBaker.BakeSH2IVGI();
        }
        
        EditorGUILayout.EndHorizontal();
    }

    private void TestInspector()
    {
        EditorGUILayout.Space();
        
        GUILayout.Label("TEST", new GUIStyle(EditorStyles.boldLabel));
        
        EditorGUILayout.PropertyField(serializedObject.FindProperty("showLightProbeVolume"));
        
        if (GUILayout.Button("Sync Test Data"))
        {
            SwithMaterialTo(_IVGIBaker._MaterialIVGI);
            
            Shader.EnableKeyword("_USE_IVGI");
            Shader.DisableKeyword("_USE_IVGI_VERTEX");
            
            Shader.DisableKeyword("_USE_IVGI_2D");
            Shader.DisableKeyword("_USE_IVGI_VERTEX_2D");
            
            Shader.SetGlobalTexture("_IVGI_LightMap", _IVGIBaker._IVGILightMap3D);
        
            Shader.SetGlobalVector("_VolumeOrigin", _IVGIBaker._VolumeOrigin);
            Shader.SetGlobalFloat("_VolumeSize", _IVGIBaker._VolumeSize);
        }
        
        if (GUILayout.Button("Sync Test Data（2D）"))
        {
            SwithMaterialTo(_IVGIBaker._MaterialIVGI);
            
            Shader.DisableKeyword("_USE_IVGI");
            Shader.DisableKeyword("_USE_IVGI_VERTEX");
            
            Shader.EnableKeyword("_USE_IVGI_2D");
            Shader.DisableKeyword("_USE_IVGI_VERTEX_2D");
            
            _IVGIBaker._IVGILightMap2D = _IVGIBaker.Texture3DTo2D(_IVGIBaker._IVGILightMap3D);
            
            Shader.SetGlobalTexture("_IVGI_LightMap2D", _IVGIBaker._IVGILightMap2D);
        
            Shader.SetGlobalVector("_VolumeOrigin", _IVGIBaker._VolumeOrigin);
            Shader.SetGlobalFloat("_VolumeSize", _IVGIBaker._VolumeSize);
        }
        
        if (GUILayout.Button("Sync Test Data（Vertex）"))
        {
            SwithMaterialTo(_IVGIBaker._MaterialIVGI);
            
            int x = 30;
            int y = 60;
            int z = 18;
            
            Color a = _IVGIBaker._IVGILightMap3D.GetPixel(x, y, z);
            
            int w = _IVGIBaker._IVGILightMap3D.width;
            int h = _IVGIBaker._IVGILightMap3D.height;
            int d = _IVGIBaker._IVGILightMap3D.depth;
            int texWidth = w * h;
            int texHeight = d;
            
            int index1D = x + y * w + z * w * h;
            int u = index1D % texWidth;
            int v = index1D / texWidth;
            
            Color b = _IVGIBaker._IVGILightMap2D.GetPixel(u, v, 0);
            
            // Shader.DisableKeyword("_USE_IVGI");
            // Shader.EnableKeyword("_USE_IVGI_VERTEX");
            //
            // Shader.SetGlobalTexture("_IVGI_LightMap", _IVGIBaker._IVGILightMap3D);
            //
            // Shader.SetGlobalVector("_VolumeOrigin", _IVGIBaker._VolumeOrigin);
            // Shader.SetGlobalFloat("_VolumeSize", _IVGIBaker._VolumeSize);
        }
        
        if (GUILayout.Button("Convert To URP LIT"))
        {
            Shader.DisableKeyword("_USE_IVGI");
            Shader.DisableKeyword("_USE_IVGI_VERTEX");
            
            SwithMaterialTo(_IVGIBaker._MaterialLit);
        }
        
        if (GUILayout.Button("IsVisible"))
        {
            Vector3 from = new Vector3(34, 61, 5);
            Vector3 to = new Vector3(34, 61, 6);
            Vector3 dir = to - from; //+ Vector3.one * 0.1f
            bool a =  !Physics.Raycast(from, dir.normalized, dir.magnitude);
            UnityEngine.Debug.Log("from:" + from + ", to:" + to + ", is " + a);
        }
    }

    private void SwithMaterialTo(Material material)
    {
        MeshRenderer[] mrs = _IVGIBaker._MeshRoot.GetComponentsInChildren<MeshRenderer>();
        for (int i = 0; i < mrs.Length; i++)
        {
            mrs[i].sharedMaterial = material;
        }
    }
}
